package com.junzhiit.nengji.cms.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.junzhiit.nengji.cms.service.RebateCategoryService;
import com.junzhiit.nengji.cms.web.exception.RebateCategoryNotFoundException;
import com.junzhiit.nengji.cms.web.exception.RebatePriceOverstepException;
import com.junzhiit.nengji.cms.web.util.ViewObjectUtil;
import com.junzhiit.nengji.cms.web.vo.DataTablesParamVO;
import com.junzhiit.nengji.cms.web.vo.RebateCategoryAddVO;
import com.junzhiit.nengji.cms.web.vo.RebateCategoryEditVO;
import com.junzhiit.nengji.cms.web.vo.RebateCategoryVO;
import com.junzhiit.nengji.cms.web.vo.ResultVO;
import com.junzhiit.nengji.dao.RebateCategoryDAO;
import com.junzhiit.nengji.model.RebateCategory;

@Service
public class RebateCategoryServiceImpl extends BaseServiceImpl implements RebateCategoryService {

	@Autowired
	private RebateCategoryDAO rebateCategoryDAO;
	
	@Override
	public Map<String, Object> findAll(DataTablesParamVO dtp, RebateCategoryVO rebateCategoryVO) {
		
		RebateCategory rebateCategory=new RebateCategory();
		BeanUtils.copyProperties(rebateCategoryVO, rebateCategory);
		
		List<RebateCategory> list=rebateCategoryDAO.findAll(rebateCategory,  dtp.getStart(), dtp.getLength());
		int totalCount = rebateCategoryDAO.getTotalCount(rebateCategory);
		return ViewObjectUtil.dataTables(list, dtp.getDraw(), totalCount);
	}
	
	@Override
	public ResultVO findById(String id){
		ResultVO result=new ResultVO();
		RebateCategory data=rebateCategoryDAO.findById(id);
		if(data==null){
			throw new RebateCategoryNotFoundException("返利类别不存在");
		}
		result.setData(data);
		return result;
	}
	
	@Override
	public ResultVO update(String id,RebateCategoryEditVO rebateCategoryEditVO){
		ResultVO result=new ResultVO();
		
		
		RebateCategory rebateCategory=rebateCategoryDAO.query(id);
		if(rebateCategory==null){
			throw new RebateCategoryNotFoundException("返利类别不存在");
		}
		
		BeanUtils.copyProperties(rebateCategoryEditVO, rebateCategory);
		rebateCategoryDAO.update(rebateCategory);
		
		RebateCategory nextRebateCategory=rebateCategoryDAO.findNextById(id);
		RebateCategory prevRebateCategory=rebateCategoryDAO.findPrevById(id);
		
		if(prevRebateCategory!=null){
			Double startPrice=rebateCategory.getStartPrice();
			if(startPrice<prevRebateCategory.getStartPrice()){
				throw new RebatePriceOverstepException("开始价格小于上一个的开始价格");
			}
			prevRebateCategory.setEndPrice(rebateCategory.getStartPrice());
			rebateCategoryDAO.update(prevRebateCategory);
		}
		
		if(nextRebateCategory!=null){
			Double endPrice=rebateCategory.getEndPrice();
			if(endPrice>nextRebateCategory.getEndPrice()){
				throw new RebatePriceOverstepException("结束价格大于下一个的结束价格");
			}
			nextRebateCategory.setStartPrice(rebateCategory.getEndPrice());
			rebateCategoryDAO.update(nextRebateCategory);
		}
		
		
		result.setData(rebateCategory);
		
		return result;
	}
	
	@Override
	public ResultVO add(RebateCategoryAddVO rebateCategoryAddVO){
		ResultVO result=new ResultVO();
		RebateCategory rebateCategory=new RebateCategory();
		BeanUtils.copyProperties(rebateCategoryAddVO, rebateCategory);
		
		Double maxPrice=rebateCategoryDAO.queryMaxPrice();
		
		if(maxPrice>=rebateCategoryAddVO.getEndPrice()){
			throw new RebatePriceOverstepException("结束价格小于已存在的最大价格");
		}
		
		rebateCategory.setStartPrice(maxPrice);
		
		RebateCategory data=rebateCategoryDAO.insert(rebateCategory);
		result.setData(data);
		return result;
	}
	
	
	@Override
	public ResultVO delete(String id){
		ResultVO result=new ResultVO();
		
		RebateCategory rebateCategory=rebateCategoryDAO.query(id);
		if(rebateCategory==null){
			throw new RebateCategoryNotFoundException("返利类别不存在");
		}
		
		RebateCategory nextRebateCategory=rebateCategoryDAO.findNextById(id);
		
		if(nextRebateCategory!=null){
			nextRebateCategory.setStartPrice(rebateCategory.getStartPrice());
			rebateCategoryDAO.update(nextRebateCategory);
		}
		
		rebateCategoryDAO.updateToDelete(id);
		
		return result;
		
	}
}
